package cn.edu.scau.cmi.wangmaoren.client.spring;


import org.springframework.context.ApplicationContext;
import af.javafx.layout.AfLinearLayout;
import cn.edu.scau.cmi.wangmaoren.spring.domain.Student;
import cn.edu.scau.cmi.wangmaoren.spring.util.ApplicationContextUtil;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SpringSingletonApplicationContextJavaFxClient extends Application
{
	private AfLinearLayout root = new AfLinearLayout();
	private AfLinearLayout line = new AfLinearLayout();
	private Button checkButton = new Button("查看学生zhangsan的导师");
	private Label blankLeft = new Label();
	private Label blankRight = new Label();
	private TextArea inforText = new TextArea();

	@Override
	public void start(Stage primaryStage)
	{
		try
		{
			initLayout();// 初始化界面

			Scene scene = new Scene(root, 750, 350);
			primaryStage.setTitle("SpringSingleton");
			primaryStage.setScene(scene);
			primaryStage.show();

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		launch(args);
	}

	private void checkTutor()
	{
		ApplicationContext ac = getApplicationContext();
		Student student = (Student) ac.getBean("zhangsan");
		inforText.setText("\n\n\n\n                                   " + student.getName() + "的毕业设计指导导师是："
				+ student.getTutor().getName());
	}

	private static ApplicationContext getApplicationContext()
	{
		ApplicationContext applicationContext = ApplicationContextUtil.getApplicationContext();
		return applicationContext;
	}

	/*
	 * 初始化界面
	 */
	private void initLayout()
	{
		root.orientation(Orientation.VERTICAL).spacing(20).padding(20);
		line.spacing(20);

		line.getChildren().addAll(blankLeft, checkButton, blankRight);
		line.layout("1 300px 1");
		root.add(line, "50px");
		root.add(inforText, "1");

		checkButton.setFont(Font.font("华文中宋", 20));
		inforText.setFont(Font.font("华文中宋", 18));

		checkButton.setOnAction((e) -> {
			checkTutor();
		});

	}
}
